package com.fcscs.cservice.client.main.configuration.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeCategory;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeCategoryModelData;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class ChargeCategoryGrid extends SimpleGridPanel<ChargeCategoryModelData> implements
		FormSubmitListener<ChargeCategory>, ModelSelectListener<ChargeCategoryModelData> {

	private static ChargeCategoryGrid INSTANCE;
	private ChargeCategoryDialog chargeCategoryDialog;

	public ChargeCategoryGrid(String heading, ModelSelectListener<ChargeCategoryModelData> l) {
		super(heading, l, (Object) null);
		setCollapsible(false);
		registerListeners(this);
		setSize(745, 520);
	}

	public static ChargeCategoryGrid getInstance(String title,
			ModelSelectListener<ChargeCategoryModelData> modelSelectListener) {
		if (null == INSTANCE) {
			INSTANCE = new ChargeCategoryGrid(title, modelSelectListener);
		}
		return INSTANCE;
	}

	@Override
	public void onModelClick(ChargeCategoryModelData model) {

	}

	@Override
	public void onModelDoubleClick(ChargeCategoryModelData model) {

	}

	@Override
	public void onModelEnterPress(ChargeCategoryModelData model) {

	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<ChargeCategory> form, SubmitType submitType) {

	}

	@Override
	protected ColumnModel createColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		ColumnConfig cc = CoreUtil.createNonSortableColumnConf(CoreConstants.FULLTNAME,
				CServiceConstants.CSERVICE_CONSTANTS.chargeCategory(), 150);
		columns.add(cc);

//		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.CODE,
//				ECMSConstants.ECMS_CONSTANTS.serviceCharge(), 120);
//		columns.add(cc);
//
//		cc = CoreUtil.createNonSortableColumnConf(ECMSConstants.C_CONF,
//				ECMSConstants.ECMS_CONSTANTS.deliveryCharge(), 120);
//		columns.add(cc);
//
//		cc = CoreUtil.createNonSortableColumnConf(ECMSConstants.DESCRIPTIONICON,
//				ECMSConstants.ECMS_CONSTANTS.salesTax(), 120);
//		columns.add(cc);
//
//		cc = CoreUtil.createNonSortableColumnConf(ECMSConstants.COOKOPTION,
//				ECMSConstants.ECMS_CONSTANTS.vat(), 120);
//		columns.add(cc);

		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
				CoreConstants.CORE_CONSTANTS.action(), 100);
		cc.setRenderer(new GridCellRenderer<ChargeCategoryModelData>() {
			public Object render(final ChargeCategoryModelData model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<ChargeCategoryModelData> store,
					final Grid<ChargeCategoryModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);

				HighLightIconButton ib = createEditBtn(model);
				HighLightIconButton id = createDeleteBtn(model, store);
				ib.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
				id.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
				ib.setId("PayTypeGrid_edit_button");
				id.setId("PayTypeGrid_dele_button");
				hr.add(ib);
				hr.add(id);
				return hr;
			}
		});
		columns.add(cc);
		ColumnModel columnModel = new ColumnModel(columns);
		return columnModel;
	}
	
	private HighLightIconButton createDeleteBtn(final ChargeCategoryModelData model,
			final ListStore<ChargeCategoryModelData> store) {
		HighLightIconButton deleBtn = new HighLightIconButton(CoreConstants.CSS_DELETE_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						MessageBox.confirm(CoreConstants.CORE_MESSAGES.operation(
								CoreConstants.CORE_CONSTANTS.delete(),
								ShareGWTConstants.SHARE_GWT_CONSTANTS.device()),
								ShareGWTConstants.SHARE_GWT_MESSAGES.whetherOperation(
										CoreConstants.CORE_CONSTANTS.delete().toLowerCase(), model
												.getEntity().getChargeCategory()),
								new Listener<MessageBoxEvent>() {

									@Override
									public void handleEvent(MessageBoxEvent be) {
										Button btn = be.getButtonClicked();
										if (Dialog.YES.equals(btn.getItemId())) {
											store.remove(model);
											CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS
													.deleted());
										}
									}
								});
					}
				});
		deleBtn.setId("DeviceGridPanel_create_deleteBtn_id");
		deleBtn.setTitle(CoreConstants.CORE_CONSTANTS.delete());
		return deleBtn;
	}

	public HighLightIconButton createEditBtn(final ChargeCategoryModelData model) {
		HighLightIconButton editBtn = new HighLightIconButton(ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {

					@Override
					public void componentSelected(IconButtonEvent ce) {
						chargeCategoryDialog = ChargeCategoryDialog.getInstance(false);
						chargeCategoryDialog.getFormPanel().updateForm(model.getEntity());
						chargeCategoryDialog.show();
					}
				});
		editBtn.setTitle(CoreConstants.CORE_CONSTANTS.delete());
		return editBtn;
	}
}
